﻿using SketchUpAPI.NET.Objects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SketchUpAPI.NET.Geometry
{
    /// <summary>
    /// 线段
    /// </summary>
    public class Line
    {
        /// <summary>
        /// 起点
        /// </summary>
        public Point3D Start { get; private set; }

        /// <summary>
        /// 终点
        /// </summary>
        public Point3D End { get; private set; }

        /// <summary>
        /// 长度
        /// </summary>
        public double Length { get => Start.DistanceTo(End); }

        /// <summary>
        /// 方向
        /// </summary>
        public Vector3D Direction
        {
            get
            {
                return (End - Start).Unit;
            }
        }

        /// <summary>
        /// 创建一个线段
        /// </summary>
        /// <param name="start">起点</param>
        /// <param name="end">终点</param>
        public Line(Point3D start, Point3D end)
        {
            Start = start;
            End = end;
        }

        /// <summary>
        /// 创建一个线段
        /// </summary>
        /// <param name="start">起点</param>
        /// <param name="direction">方向</param>
        /// <param name="length">长度</param>
        public Line(Point3D start, Vector3D direction, double length)
        {
            Start = start;
            End = start + direction.Unit * length;
        }

        /// <summary>
        /// 对应长度处的点
        /// </summary>
        /// <param name="length">与起点的距离</param>
        /// <returns></returns>
        public Point3D PointAtLength(double length)
        {
            return Start + Direction * length;
        }

        /// <summary>
        /// 参数对应的点
        /// </summary>
        /// <param name="t">[0,1]，0表示起点，1表示终点</param>
        /// <returns></returns>
        public Point3D PointAt(double t)
        {
            return Start + Direction * t * Length;
        }

        public static implicit operator Vector3D(Line line) => line.End - line.Start;
        public static implicit operator double(Line line) => line.Length;
        public static implicit operator Edge(Line line) => new Edge(line.Start, line.End);
        public static implicit operator Curve(Line line) => new Curve(line);
    }
}